import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class 控股参股分公司
{
    public static void main(String[] args){
        MongoClient mongoClient_t=new MongoClient("192.168.1.171",30000);
        MongoCollection<org.bson.Document> collection = mongoClient_t.getDatabase("Choice_ccg").getCollection("choice_konggucangufengongsi_2021");


        String path="D:\\测试临时文件\\20190605_ccg_参控股公司列表\\上市公司股票代码列表.xlsx";  //打开保存的URL路径
        //List<String> Symbol_List=new ArrayList<String>();
        try{
            InputStream input=new FileInputStream(path);
            XSSFWorkbook wwb=new XSSFWorkbook(input);
            XSSFSheet xssfSheet=wwb.getSheetAt(0);
            for(int i=1;i<=xssfSheet.getLastRowNum();i++){
                System.out.println("读取第--"+i+"--条数据"+xssfSheet.getRow(i).getCell(0)+"".trim());
//                Random random=new Random();
//                int random_time=random.nextInt(10)+9;
//                Thread.sleep(random_time*200);
                parseCode(xssfSheet.getRow(i).getCell(0)+"".trim(),collection);
            }
        }catch(Exception e){
            e.printStackTrace();
        }



    }


    private static void parseCode(String code, MongoCollection<org.bson.Document> collection){


        String url="http://app2.jg.eastmoney.com/stock/f9/GetEquityControlledCompanyList?SecurityCode="+code+"&StartDate=2020-01-01&EndDate=2020-12-31&ReportPeriodType=1%2C5%2C3%2C6&Order=desc&baoGaoQi=&baoGaoLeiXing=&clientId=1";

        String cookie="ct=L1Z0o2t_zzuNNAVz27qzNyYXrmpC4R6AXvzKP2HS9kETYDz74XLqKL03l90NcjHS0SnQM5iNXHAsXbqp3MHyqldiG_oaApu-pTMFJ134RUjS6PldqlLeoD4fTwZadDhzizgTPwdjESwzuncRYLhK8QP50tf088ED74qUpcdUt5c; ut=FobyicMgeV76LpUbJjOKMhT0UdCW9-7GyG4PZN4c1jAb-gU46b7v178QFeAMBIYvnGGyQuu6UKPKrqVnWA1dA-ED2lvLO-fHLLzzaX-vMKmJPa8KkJmoZp2u9jotCzQeYINTmod4sUCyiHmWThnkL3r6IhWO8Uwrstu9cLuOISGNsxsrNSAZU62HjJIJq55p-I2VVseCt6O6T3hHmWK823iwQhUHiW_66IAfymtoJYwAQ-S8XI-5_qG3zDvViJJOHCbKAJE66BVSdaUgRrlMt_nHmLisB-yBTnBUdy5a_JTY418uI1E6nYgMFtHtLOvPGB_5DMYVL_8; pi=4939035350876486%3bw4939035350876486%3b%e8%82%a1%e5%8f%8bboYLUM%3bDpr1sBETBqJcEJePNg4jOccRDDNEjZ2Go8I941fQh7Zhe%2bcYXjS2XNrzd25Y9eb8dt1g5J%2fZfsgOd5nz6IdVhex5ly%2bUCcJBEHECFlw%2fDDhhp%2bQ4LuvBC4bzarGvtYFi0GFmo2dQ%2bZ23zC6Kodw55Zo%2bxJhtgVk57%2bTSj6HfxmQm0Ycs9aWOPCP73cJhDpzDRfOAKas%2f%3bOT5ye12FvZSc3qm8z2jsMqaqK7IiKlp5c5jKst2atVykGkvEaciOqC9NqoeKtYbZz6mxzCRnheCFb%2bE1UZw1BfqlZWPTR5raT6x4PLX5Fr06hQ70wB4jIq7mv3oVEwWrk3PF0L0wJBffUjo7rcelkAu2e%2fcrHA%3d%3d";

        //设置client客户端
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(360, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();

        try{
            Request request = new Request.Builder().url(url)
                    .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36")
                    .addHeader("Referer", "http://app2.jg.eastmoney.com/stock/cn/f9/index.html")
                    .addHeader("Host","app2.jg.eastmoney.com")
                    .addHeader("Connection", "keep-alive")
                    .addHeader("Cookie", cookie)
                    .build();


            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                try {
                    Document doc= Jsoup.parse(response.body().string());
                    String body=doc.body().toString().replace("<body>","").replace("</body>","");

                    JSONObject elements= (JSONObject) JSONArray.fromObject(body).get(0);
                    JSONArray details= (JSONArray) elements.get("Details");
                    org.bson.Document data=new org.bson.Document();


                    for(int i=0;i<details.size();i++){
                        org.bson.Document each_data=new org.bson.Document();
                        //System.out.println(details.get(i));
                        JSONObject detail= (JSONObject) details.get(i);
                        Iterator<String> iterator=detail.keys();
                        while(iterator.hasNext()){
                            String key=iterator.next();
                            String value=detail.getString(key);
                            //System.out.println("key:"+key+"-value:"+value);
                            each_data.append(key,value);
                        }
                        data.append(String.valueOf(i),each_data);
                    }

                    collection.updateOne(new org.bson.Document("_id",code),new org.bson.Document("$set",
                                    new org.bson.Document("data",data))
                            , new UpdateOptions().upsert(true));
                } catch (Exception e) {
                   e.printStackTrace();
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("---错误的url---"+url);
        }
    }


}
